//
//  ModifyPasswordViewController.m
//  HWZB
//
//  Created by 许成雄 on 2017/4/24.
//  Copyright © 2017年 wonders. All rights reserved.
//

#import "ModifyPasswordViewController.h"
#import "HWZBAPIRequest.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "MBProgressHUD+Toast.h"
#import "NSString+Utils.h"
#import "RegexKit.h"
#import "Context.h"
#import "VerifyButton.h"

@interface ModifyPasswordViewController ()

@property (strong, nonatomic) UITextField *mobilePhoneTextField;
@property (strong, nonatomic) UITextField *verifyCodeTextField;
@property (strong, nonatomic) VerifyButton *verifyCodeButton;
@property (strong, nonatomic) UITextField *passwordTextField;
@property (strong, nonatomic) UITextField *currentPasswordTextField;
@property (strong, nonatomic) UIButton *commitButton;

@end

@implementation ModifyPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = I_COLOR_WHITE;
    [self setBackNavgationItem];
    self.navigationItem.title = @"修改密码";
    
    [self createUI];
    [self loadData];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    if(self.verifyCodeButton) {
         [self.verifyCodeButton stop];
    }
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - Private Method
- (void)createUI {
    
    UIImageView *mobilePhoneImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(12.0f), TRANS_VALUE(9.0f), TRANS_VALUE(16.0f), TRANS_VALUE(22.0f))];
    mobilePhoneImageView.backgroundColor = I_COLOR_WHITE;
    mobilePhoneImageView.contentMode = UIViewContentModeScaleAspectFit;
    mobilePhoneImageView.image = [UIImage imageNamed:@"ic_login_mobile_phone_1"];
    [self.view addSubview:mobilePhoneImageView];
    
    UILabel *mobilePhoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(40.0f), TRANS_VALUE(0.0f), TRANS_VALUE(60.0f), TRANS_VALUE(40.0f))];
    mobilePhoneLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    mobilePhoneLabel.textColor = I_COLOR_BLACK;
    mobilePhoneLabel.text = @"手机号";
    [self.view addSubview:mobilePhoneLabel];
    
    self.mobilePhoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(105.0f), TRANS_VALUE(0.0f), TRANS_VALUE(205.0f), TRANS_VALUE(40.0f))];
    self.mobilePhoneTextField.borderStyle = UITextBorderStyleNone;
    self.mobilePhoneTextField.backgroundColor = I_COLOR_WHITE;
    self.mobilePhoneTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    self.mobilePhoneTextField.textColor = I_COLOR_BLACK;
    self.mobilePhoneTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入手机号" attributes:@{NSForegroundColorAttributeName:I_COLOR_GRAY}];
    self.mobilePhoneTextField.tintColor = I_COLOR_BLACK;
    self.mobilePhoneTextField.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:self.mobilePhoneTextField];
    self.mobilePhoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    
    UIView *dividerView1 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(40.0f), SCREEN_WIDTH, 1.0f)];
    dividerView1.backgroundColor = I_COLOR_DIVIDER;
    [self.view addSubview:dividerView1];
    
    UIImageView *verifyCodeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(10.5f), TRANS_VALUE(10.5f) + TRANS_VALUE(40.0f) + 1.0f , TRANS_VALUE(19.0f), TRANS_VALUE(19.0f))];
    verifyCodeImageView.backgroundColor = I_COLOR_WHITE;
    verifyCodeImageView.contentMode = UIViewContentModeScaleAspectFit;
    verifyCodeImageView.image = [UIImage imageNamed:@"ic_login_verify_code_1"];
    [self.view addSubview:verifyCodeImageView];
    
    UILabel *verifyCodeLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(40.0f), TRANS_VALUE(40.0f) + 1.0f, TRANS_VALUE(60.0f), TRANS_VALUE(40.0f))];
    verifyCodeLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    verifyCodeLabel.textColor = I_COLOR_BLACK;
    verifyCodeLabel.text = @"验证码";
    [self.view addSubview:verifyCodeLabel];
    
    self.verifyCodeTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(105.0f), TRANS_VALUE(40.0f) + 1.0f, TRANS_VALUE(125.0f), TRANS_VALUE(40.0f))];
    self.verifyCodeTextField.borderStyle = UITextBorderStyleNone;
    self.verifyCodeTextField.backgroundColor = I_COLOR_WHITE;
    self.verifyCodeTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    self.verifyCodeTextField.textColor = I_COLOR_BLACK;
    self.verifyCodeTextField.textColor = I_COLOR_BLACK;
    self.verifyCodeTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:@{NSForegroundColorAttributeName:I_COLOR_GRAY}];
    self.verifyCodeTextField.tintColor = I_COLOR_BLACK;
    self.verifyCodeTextField.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:self.verifyCodeTextField];
    self.verifyCodeTextField.keyboardType = UIKeyboardTypeNumberPad;
    
    UIView *dividerView2 = [[UIView alloc] initWithFrame:CGRectMake(TRANS_VALUE(230.0f), TRANS_VALUE(40.0f) + 1.0f + TRANS_VALUE(5.0f)  ,TRANS_VALUE(1.0f), TRANS_VALUE(30.0f))];
    dividerView2.backgroundColor = I_COLOR_GRAY;
    [self.view addSubview:dividerView2];
    
    self.verifyCodeButton = [[VerifyButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - TRANS_VALUE(88.0f), TRANS_VALUE(40.0f) + 1.0f, TRANS_VALUE(87.0f), TRANS_VALUE(40.0f)) withTitle:@"获取验证码"];
    [self.verifyCodeButton setBackgroundColor:I_COLOR_WHITE];
    [self.verifyCodeButton setTitleColor:I_COLOR_BLACK];
    [self.view addSubview:self.verifyCodeButton];
    [self.verifyCodeButton addTarget:self action:@selector(verifyCodeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *dividerView3 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(80.0f) + 2.0f, SCREEN_WIDTH, 1.0f)];
    dividerView3.backgroundColor = I_COLOR_DIVIDER;
    [self.view addSubview:dividerView3];
    
    UIImageView *passwordImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(12.5f), TRANS_VALUE(80.0f) + 2.0f + TRANS_VALUE(8.0f), TRANS_VALUE(15.0f), TRANS_VALUE(24.0f))];
    passwordImageView.contentMode = UIViewContentModeScaleAspectFit;
    passwordImageView.image = [UIImage imageNamed:@"ic_login_password_1"];
    [self.view addSubview:passwordImageView];
    
    UILabel *passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(40.0f), TRANS_VALUE(80.0f) + 2.0f, TRANS_VALUE(60.0f), TRANS_VALUE(40.0f))];
    passwordLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    passwordLabel.textColor = I_COLOR_BLACK;
    passwordLabel.text = @"原密码";
    [self.view addSubview:passwordLabel];
    
    self.passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(105.0f), TRANS_VALUE(80.0f) + 3.0f, TRANS_VALUE(205.0f), TRANS_VALUE(40.0f))];
    self.passwordTextField.borderStyle = UITextBorderStyleNone;
    self.passwordTextField.backgroundColor = I_COLOR_WHITE;
    self.passwordTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    self.passwordTextField.textColor = I_COLOR_BLACK;
    self.passwordTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入原密码" attributes:@{NSForegroundColorAttributeName:I_COLOR_GRAY}];
    self.passwordTextField.tintColor = I_COLOR_BLACK;
    self.passwordTextField.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:self.passwordTextField];
    [self.passwordTextField setSecureTextEntry:YES];
    
    UIView *dividerView4 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(120.0f) + 2.0f, SCREEN_WIDTH, 1.0f)];
    dividerView4.backgroundColor = I_COLOR_DIVIDER;
    [self.view addSubview:dividerView4];
    
    UIImageView *retryPasswordImageView = [[UIImageView alloc] initWithFrame:CGRectMake(TRANS_VALUE(12.5f), TRANS_VALUE(120.0f) + 3.0f + TRANS_VALUE(8.0f), TRANS_VALUE(15.0f), TRANS_VALUE(24.0f))];
    retryPasswordImageView.contentMode = UIViewContentModeScaleAspectFit;
    retryPasswordImageView.image = [UIImage imageNamed:@"ic_login_password_1"];
    [self.view addSubview:retryPasswordImageView];
    
    UILabel *retryPasswordLabel = [[UILabel alloc] initWithFrame:CGRectMake(TRANS_VALUE(40.0f), TRANS_VALUE(120.0f) + 3.0f, TRANS_VALUE(60.0f), TRANS_VALUE(40.0f))];
    retryPasswordLabel.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    retryPasswordLabel.textColor = I_COLOR_BLACK;
    retryPasswordLabel.text = @"新密码";
    [self.view addSubview:retryPasswordLabel];

    self.currentPasswordTextField = [[UITextField alloc] initWithFrame:CGRectMake(TRANS_VALUE(105.0f), TRANS_VALUE(120.0f) + 3.0f, TRANS_VALUE(205.0f), TRANS_VALUE(40.0f))];
    self.currentPasswordTextField.borderStyle = UITextBorderStyleNone;
    self.currentPasswordTextField.backgroundColor = I_COLOR_WHITE;
    self.currentPasswordTextField.font = [UIFont systemFontOfSize:TRANS_VALUE(13.0f)];
    self.currentPasswordTextField.textColor = I_COLOR_BLACK;
    self.currentPasswordTextField.textAlignment = NSTextAlignmentLeft;
    self.currentPasswordTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入新密码" attributes:@{NSForegroundColorAttributeName:I_COLOR_GRAY}];
    self.currentPasswordTextField.tintColor = I_COLOR_BLACK;
    [self.view addSubview:self.currentPasswordTextField];
    [self.currentPasswordTextField setSecureTextEntry:YES];
    
    UIView *dividerView5 = [[UIView alloc] initWithFrame:CGRectMake(0, TRANS_VALUE(160.0f) + 3.0f, SCREEN_WIDTH, 1.0f)];
    dividerView5.backgroundColor = I_COLOR_DIVIDER;
    [self.view addSubview:dividerView5];
    
    self.commitButton = [[UIButton alloc] initWithFrame:CGRectMake(TRANS_VALUE(30.0f), TRANS_VALUE(160.0f) + 3.0f + TRANS_VALUE(20.0f), SCREEN_WIDTH - 2 * TRANS_VALUE(30.0f), TRANS_VALUE(36.0f))];
    self.commitButton.clipsToBounds = YES;
    self.commitButton.layer.borderColor = I_COLOR_GRAY.CGColor;
    self.commitButton.layer.borderWidth = 1.0f;
    self.commitButton.titleLabel.font = [UIFont boldSystemFontOfSize:TRANS_VALUE(14.0f)];
    [self.commitButton setTitleColor:I_COLOR_BLACK forState:UIControlStateNormal];
    [self.commitButton setTitle:@"提交修改" forState:UIControlStateNormal];
    [self.view addSubview:self.commitButton];
    
    [self.commitButton addTarget:self action:@selector(commitButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)loadData {
    //TODO -- 加载数据
    
}

#pragma mark - UIButton Action
//验证码按钮
- (void)verifyCodeButtonAction:(id)sender {
    NSString *mobile = self.mobilePhoneTextField.text;
    if([NSString isBlankString:mobile]) {
        [MBProgressHUD showToast:@"请输入手机号码" initWithView:self.view];
        return;
    } else if(![RegexKit validateMobile:mobile]) {
        [MBProgressHUD showToast:@"请输入正确的手机号码" initWithView:self.view];
        return;
    }
    VerifyButton *button = (VerifyButton *)sender;
    [button start];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    [params setObject:mobile forKey:@"mobile"];
    [SVProgressHUD showWithStatus:@"正在获取验证码..." maskType:SVProgressHUDMaskTypeClear];
    [[HWZBAPIRequest sharedInstance] getVerifyCodeWithParams:params success:^(NSString *verifyCode) {
        [SVProgressHUD dismiss];
        if(verifyCode != nil) {
            [MBProgressHUD showToast:@"已发送验证码, 请注意查收" initWithView:self.view];
        } else {
            [MBProgressHUD showToast:@"获取验证码失败" initWithView:self.view];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
        [MBProgressHUD showToast:[error localizedDescription] initWithView:self.view];
    }];

}

//提交修改按钮点击事件
- (void)commitButtonAction:(id)sender {
    NSString *mobile = self.mobilePhoneTextField.text;
    NSString *verifyCode = self.verifyCodeTextField.text;
    NSString *password = self.passwordTextField.text;
    NSString *newPassword = self.currentPasswordTextField.text;
    if([NSString isBlankString:mobile]) {
        [MBProgressHUD showToast:@"请输入手机号码" initWithView:self.view];
        return;
    } else if(![RegexKit validateMobile:mobile]) {
        [MBProgressHUD showToast:@"请输入正确的手机号码" initWithView:self.view];
        return;
    }
    if([NSString isBlankString:verifyCode]) {
        [MBProgressHUD showToast:@"请输入验证码" initWithView:self.view];
        return;
    }
    if([NSString isBlankString:password]) {
        [MBProgressHUD showToast:@"请输入原密码" initWithView:self.view];
        return;
    }
    if([NSString isBlankString:newPassword]) {
        [MBProgressHUD showToast:@"请输入新密码" initWithView:self.view];
        return;
    } else if([password isEqualToString:newPassword]) {
        [MBProgressHUD showToast:@"新密码和原密码一样, 请重新输入" initWithView:self.view];
        return;
    } else if(password.length < 6 || password.length > 18) {
        [MBProgressHUD showToast:@"密码过短，正常长度为6~18位" initWithView:self.view];
        return;
    }
    
    //用户修改密码
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    [params setObject:mobile forKey:@"mobile"];
    [params setObject:password forKey:@"password"];  //旧密码
    [params setObject:verifyCode forKey:@"checkRand"];
    [params setObject:newPassword forKey:@"repeatPassword"];
    [SVProgressHUD showWithStatus:@"正在修改用户密码..." maskType:SVProgressHUDMaskTypeClear];
    [[HWZBAPIRequest sharedInstance] modifyPasswordWithParams:params success:^(CommonInfo *resultInfo) {
        [SVProgressHUD dismiss];
        if(resultInfo != nil && [resultInfo isSuccess]) {
            [MBProgressHUD showToast:@"修改密码成功" initWithView:self.view];
            [self performSelector:@selector(dismissThisViewController) withObject:nil afterDelay:1.0f];
        } else {
            [MBProgressHUD showToast:@"修改密码失败，请稍后再试" initWithView:self.view];
        }
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
        [MBProgressHUD showToast:[error localizedDescription] initWithView:self.view];
    }];
}

- (void)dismissThisViewController {
    [self.navigationController popViewControllerAnimated:YES];
    
}

@end
